package center

import (
	"fmt"
	"github.com/gin-gonic/gin"
	_ "github.com/go-sql-driver/mysql"
	"github.com/jmoiron/sqlx"
	"net/http"
	"time"
)

type RegisterInfo struct {
	PetName    string `db:"pet_name"`
	PetAge     string `db:"pet_age"`
	PeopleName string `db:"people_name"`
	PeopleTel  string `db:"people_tel"`
	PetDisease string `db:"pet_disease"`
	Remarks    string `db:"remarks"`
}

var Db *sqlx.DB

func init() {
	database, err := sqlx.Open("mysql", "root:123456@tcp(127.0.0.1:3306)/go")
	if err != nil {
		fmt.Println("open mysql failed", err)
		//defer Db.Close()
		return
	}
	Db = database
}

//宠物信息登记
func petInfoRegister(c *gin.Context) {
	var json RegisterInfo
	if err := c.ShouldBindJSON(&json); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		c.JSON(203, gin.H{"status": 203, "message": "信息提交失败"})
	}

	fmt.Println("aaaaaa", json, time.Now())
	r, err := Db.Exec("insert into pet_info(pet_name, pet_age, people_name, people_tel, pet_disease, remarks, nowtime)values(?, ?, ?, ?, ?, ?, ?)", json.PetName, json.PetAge, json.PeopleName, json.PeopleTel, json.PetDisease, json.Remarks, time.Now())
	if err != nil {
		fmt.Println("exec failed, ", err)
		c.JSON(203, gin.H{"status": 203, "message": "信息提交失败"})
		return
	}
	id, err := r.LastInsertId()
	if err != nil {
		fmt.Println("exec failed, ", err)
		c.JSON(203, gin.H{"status": 203, "message": "信息提交失败"})
		return
	}
	fmt.Println(id)
	c.JSON(200, gin.H{"status": 200, "message": "信息提交成功"})
}

//宠物信息查询
func petInfoSearch(c *gin.Context) {
	searchnews := c.Param("searchnews")
	fmt.Println(searchnews)
	var json2 []RegisterInfo
	err := Db.Select(&json2, "select pet_name, pet_age, people_name, people_tel, pet_disease, remarks from pet_info where pet_age=? or people_name=? or people_tel=?", searchnews, searchnews, searchnews)
	if err != nil {
		fmt.Println("exec failed, ", json2, err)
		c.JSON(203, gin.H{"status": 203, "message": "信息查询失败"})
		return
	}
	fmt.Println(json2)
	c.JSON(200, gin.H{"status": 200, "message": "信息查询成功", "data": json2})
}

type UpdateInfo struct {
	PetName       string
	PeopleName    string
	PeopleTel     string
	NewPeopleName string
}

//宠物信息更新
func updateInfo(c *gin.Context) {
	var json UpdateInfo
	if err := c.ShouldBindJSON(&json); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		c.JSON(203, gin.H{"status": 203, "message": "信息提交失败"})
	}
	r, err := Db.Exec("update pet_info set people_name=? where pet_name=? and people_name=? and people_tel=?", json.NewPeopleName, json.PetName, json.PeopleName, json.PeopleTel)
	if err != nil {
		fmt.Println("exec failed, ", err)
		return
	}
	row, err := r.RowsAffected()
	if err != nil {
		fmt.Println("rows failed, ", err)
	}
	fmt.Println("update succ:", row)
}

//宠物信息删除
func deleteInfo(c *gin.Context) {
	name := c.PostForm("deletename")
	res, err := Db.Exec("delete from pet_info where pet_name=?", name)
	if err != nil {
		fmt.Println("exec failed, ", err)
		c.JSON(203, gin.H{"status": 203, "message": "信息删除失败"})
		return
	}

	row, err := res.RowsAffected()
	if err != nil {
		fmt.Println("rows failed, ", err)
	}
	fmt.Println("delete succ: ", row)
	c.JSON(200, gin.H{"status": 200, "message": "信息删除成功"})
}
